package com.controller;

import com.bean.Flower;
import com.bean.Page;
import com.service.FlowerService;
import com.service.impl.FlowerServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class ClientFlowerServlet extends BaseServlet {

    private FlowerService flowerService = new FlowerServiceImpl();

    /**
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getPageAllFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取请求参数
        // System.out.println("经过了");
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用flowerservice方法
        Page<Flower> page = flowerService.page(pageNo, pageSize);
        page.setUrl("client/flowerServlet?action=getPageAllFlower");
        req.setAttribute("page", page);
        //resp.sendRedirect("page/user/indexUser.jsp");
        req.getRequestDispatcher("/page/user/indexUser.jsp").forward(req, resp);
    }

    /**
     * 按价格区间查找鲜花
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        //System.out.println("经过了");
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(req.getParameter("min"), 0);
        int max = WebUtils.parseInt(req.getParameter("max"), Integer.MAX_VALUE);
        //调用flowerservice方法
        Page<Flower> page = flowerService.pageByPrice(pageNo, pageSize, min, max);

        StringBuffer sb = new StringBuffer("client/flowerServlet?action=pageByPrice");
        if (req.getParameter("min") != null) {
            sb.append("&min=").append(req.getParameter("min"));
        }
        if (req.getParameter("max") != null) {
            sb.append("&max=").append(req.getParameter("max"));
        }
        page.setUrl(sb.toString());
        //
        req.setAttribute("page", page);
        //
        req.getRequestDispatcher("/page/user/indexUser.jsp").forward(req, resp);
    }

    /**
     * 按鲜花名称模糊查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String flower_name = req.getParameter("flower_name");
        Page<Flower> page = flowerService.pageByName(pageNo, pageSize, flower_name);
        page.setUrl("client/flowerServlet?action=pageByName");
        StringBuffer sb = new StringBuffer("client/flowerServlet?action=pageByName");
        if (req.getParameter("flower_name") != null) {
            sb.append("&flower_name=").append(req.getParameter("flower_name"));
        }
        page.setUrl(sb.toString());
        req.setAttribute("page", page);
        req.getRequestDispatcher("/page/user/indexUser.jsp").forward(req, resp);
        //resp.sendRedirect(req.getContextPath()+"/page/user/indexUser.jsp");
    }

    /**
     * 根据鲜花类别查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryByFclassid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        String fid = req.getParameter("fclassid");
        Integer fclassid = Integer.parseInt(fid);
        Page<Flower> page = flowerService.queryByFclassid(pageNo, pageSize, fclassid);
        page.setUrl("client/flowerServlet?action=queryByFclassid");
        StringBuffer sb = new StringBuffer("client/flowerServlet?action=queryByFclassid");
        if (req.getParameter("fclassid") != null) {
            sb.append("&fclassid=").append(req.getParameter("fclassid"));
        }
        page.setUrl(sb.toString());
        req.setAttribute("page", page);
        req.getRequestDispatcher("/page/user/indexUser.jsp").forward(req, resp);
    }

    /**
     * 查看鲜花产品详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showFlowerDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        int flowerid = WebUtils.parseInt(req.getParameter("flowerid"), 0);
        //调用servkce
        List<Map<String, Object>> flowers = flowerService.queryFlowerid(flowerid);
        //保存到request
        System.out.println(flowers);
        /*  List<Flower> flowerList=WebUtils.toFlowerItem(flowers);*/
        req.setAttribute("flowers", flowers);
        //转发
        req.getRequestDispatcher("/page/cmain/flowerdetail.jsp").forward(req, resp);
    }

    /**
     * 根据销量查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryBySales(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Flower> flowers = null;
        flowers = flowerService.queryBySales();
        System.out.println(flowers);
        req.setAttribute("flowers", flowers);
        req.getRequestDispatcher("/page/comment/special.jsp").forward(req, resp);
    }

    /**
     * 游客查询
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void noquery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String content = req.getParameter("content");
        if (WebUtils.isContainChinese(content)) {
            List<Map<String, Object>> flowers = null;
            flowers = flowerService.queryByName(content);
            req.setAttribute("flowers",flowers);
            System.out.println(flowers);
            req.getRequestDispatcher("/page/cmain/search.jsp").forward(req,resp);
        } else if (WebUtils.checkStrIsNum(content)) {
            List<Flower> flowers = null;
            BigDecimal price = new BigDecimal(content);
            flowers = flowerService.queryByPrice(price);
            System.out.println(flowers);
            req.setAttribute("flowers",flowers);
            req.getRequestDispatcher("/page/cmain/search.jsp").forward(req, resp);
        }else{
            req.getRequestDispatcher("/page/cmain/search.jsp").forward(req, resp);
        }
    }

    /**
     * 特殊节日
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void querySpecial(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String content="母亲节";
        List<Map<String, Object>> flowers = null;
        flowers = flowerService.queryByName(content);
        req.setAttribute("flowers",flowers);
        System.out.println(flowers);
        //req.getRequestDispatcher("/page/cmain/search.jsp").forward(req,resp);
        req.getRequestDispatcher("/page/comment/special.jsp").forward(req, resp);
    }
}
